//
//  ASTeacherCallNameViewController.m
//  AttendenceSys
//
//  Created by ld on 16/1/28.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASTeacherCallNameViewController.h"

@interface ASTeacherCallNameViewController ()

@end

@implementation ASTeacherCallNameViewController

-(void)viewWillDisappear:(BOOL)animated
{
   [self removeBackView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _weekDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"周一",@"1",@"周二",@"2",@"周三",@"3",@"周四",@"4",@"周五",@"5",@"周六",@"6",@"周日",@"7", nil];
    //self.strNavTitle = [self getYearMonthDay:[NSDate date]];
    self.strNavTitle = @"点名";
    self.subClaInfo = [[SubClassInfo alloc] init];
    //_appselectArr = [[NSMutableArray alloc] init];
    _sDate = [self.strNavTitle stringByReplacingOccurrencesOfString:@"." withString:@"-"];
    _subjectselectDate =  _subjecteaName = @"";
    [self initData];
    [self initView];
    
    // Do any additional setup after loading the view.
}

-(NSString*)getYearMonthDay:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:date];
    NSString* year = [NSString stringWithFormat:@"%ld",[dateComponent year]];
    NSString* month = [NSString stringWithFormat:@"%ld",[dateComponent month]];
    NSString* day = [NSString stringWithFormat:@"%ld",[dateComponent day]];
    NSString* yearMonthDay = [NSString stringWithFormat:@"%@.%@.%@",year,month,day];
    return yearMonthDay;
}

-(void)initDatePickerView
{
    _dateSelctPickerView = [[ASDatePickerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-216-46, [UIScreen mainScreen].bounds.size.width, 216+46)];
    _dateSelctPickerView.iTag = 0;
    _dateSelctPickerView.editTimeLab.text = @"选择日期";
    _dateSelctPickerView.delegate = self;
    _dateSelctPickerView.datePickerView.datePickerMode = UIDatePickerModeDate;
    NSDate* minDate = [NSDate date];
    //NSDate* maxDate = [[NSDate alloc]initWithString:@"2099-01-01 00:00:00 -0500"];
    
    _dateSelctPickerView.datePickerView.minimumDate = minDate;
    //datePicker.maximumDate = maxDate;
    
    [self.view addSubview:_dateSelctPickerView];
    
}

-(void)initData
{
    //_teaCallNameInfoArr = [[NSMutableArray alloc] initWithObjects:@"选择科目名称", @"选择班级", nil];
    _teaCallNameInfoArr = [[NSMutableArray alloc] initWithObjects:@"选择老师",@"选择点名日期",@"选择科目名称", nil];
}

-(void)initView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = YES;
    [_tableView registerClass:[AskForLeaTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AskForLeaTableViewCell class])];
    [self.view addSubview:_tableView];
    
    
    //next
    
    //开始点名按钮
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, SubClassCellHeight*_teaCallNameInfoArr.count, CGRectGetWidth(self.view.bounds), 100)];
    footer.backgroundColor = [UIColor clearColor];
    
    UIButton* callNameBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //        callNameBtn.frame = CGRectMake(50, [UIScreen mainScreen].bounds.size.width/2+50,[UIScreen mainScreen].bounds.size.width-50*2,50);
    callNameBtn.frame = CGRectMake(50, 50,[UIScreen mainScreen].bounds.size.width-50*2,50);
    [callNameBtn setTitle:@"开始点名" forState:UIControlStateNormal];
    callNameBtn.layer.cornerRadius = 4.5;
    [callNameBtn addTarget:self action:@selector(callName) forControlEvents:UIControlEventTouchUpInside];
    
    [callNameBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    callNameBtn.backgroundColor = RGB(252, 120, 61);
    callNameBtn.enabled = YES;
    [footer addSubview:callNameBtn];
    _tableView.tableFooterView = footer;
}

- (UIView*)topView
{
    UIViewController *recentView = self;
    
    while (recentView.parentViewController != nil)
    {
        recentView = recentView.parentViewController;
    }
    return recentView.view;
}

//删除背景视图
- (void)removeBackView
{
    if (_backgroundView)
    {
        [_backgroundView removeFromSuperview];
        _backgroundView = nil;
    }
}

#pragma mark - ASDatePickerViewDelegate
- (void)datePickerCancel
{
    [self removeBackView];
}

- (void)dataPickSelect:(NSInteger)selectTag selectDate:(NSString*)selectData
{
    NSDate* date = [self convertDateFromString:selectData];
    _subjectselectDate = [self getYearMonthDay:date];
    [self.tableView reloadData];
    [self removeBackView];
}

- (NSDate*)convertDateFromString:(NSString*)uiDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *date = [dateFormatter dateFromString:uiDate];
    NSLog(@"%@", date);
    
    return date;
}

//开始点名
-(void)callName
{
    if (0 == _subjectNameLab.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请选择要点名的课程"];
        return;
    }
    /*
    if (0 == _subjectTimeLab.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请选择要点名的日期"];
        return;
    }
     */
    ASTeacherCallNameConfirmViewController* callNameConfirmViewCtrl = [[ASTeacherCallNameConfirmViewController alloc] init];
    callNameConfirmViewCtrl.hidesBottomBarWhenPushed = YES;
    callNameConfirmViewCtrl.userTeaCode = _appClaTeaInfo.userCode;
    callNameConfirmViewCtrl.subClsInfo = _appClaClaInfo;
    callNameConfirmViewCtrl.strNavTitle = [NSString stringWithFormat:@"点名--%@",_appClaTeaInfo.userName];
    [self.navigationController pushViewController:callNameConfirmViewCtrl animated:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_teaCallNameInfoArr count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AskForLeaTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AskForLeaTableViewCell class])];
    [cell setBackgroundColor:[UIColor clearColor]];
    cell.titleLabel.text = [_teaCallNameInfoArr objectAtIndex:indexPath.row];
    if (0 == indexPath.row) {
        if (!_subjectTeaLab)
        {
            _subjectTeaLab = [[UILabel alloc] initWithFrame:CGRectMake(cell.imgView.frame.origin.x - [UIScreen mainScreen].bounds.size.width/3 - 40 - 10.5 , 12+10, 150, cell.titleLabel.frame.size.height)];
            [_subjectTeaLab setBackgroundColor:[UIColor clearColor]];
            _subjectTeaLab.text = self.subClaInfo.courseName;
            _subjectTeaLab.font = [UIFont systemFontOfSize:18];
            [_subjectTeaLab setTextAlignment:NSTextAlignmentRight];
            _subjectTeaLab.textColor = RGB(102, 102, 102);
            [cell addSubview:_subjectTeaLab];
        }
        
        _subjectTeaLab.text = _subjecteaName;
    }
    if (1 == indexPath.row) {
        if (!_subjectTimeLab)
        {
            _subjectTimeLab = [[UILabel alloc] initWithFrame:CGRectMake(cell.imgView.frame.origin.x - [UIScreen mainScreen].bounds.size.width/3 - 40 - 10.5 , 12+10, 150, cell.titleLabel.frame.size.height)];
            [_subjectTimeLab setBackgroundColor:[UIColor clearColor]];
            _subjectTimeLab.text = self.subClaInfo.courseName;
            _subjectTimeLab.font = [UIFont systemFontOfSize:18];
            [_subjectTimeLab setTextAlignment:NSTextAlignmentRight];
            _subjectTimeLab.textColor = RGB(102, 102, 102);
            [cell addSubview:_subjectTimeLab];
        }
        
        _subjectTimeLab.text = _subjectselectDate;
    }
    else if (2 == indexPath.row) {
        //cell.imgView.hidden = YES;
        //cell.imgView.hidden = YES;
        if (!_subjectNameLab)
        {
            _subjectNameLab = [[UILabel alloc] initWithFrame:CGRectMake(cell.imgView.frame.origin.x - [UIScreen mainScreen].bounds.size.width/3 - 40 - 10.5 , 12+10, 150, cell.titleLabel.frame.size.height)];
            [_subjectNameLab setBackgroundColor:[UIColor clearColor]];
            //_subjectNameLab.text = self.subClaInfo.courseName;
            _subjectNameLab.font = [UIFont systemFontOfSize:15];
            [_subjectNameLab setTextAlignment:NSTextAlignmentRight];
            _subjectNameLab.textColor = RGB(102, 102, 102);
            [cell addSubview:_subjectNameLab];
        }
        
        _subjectNameLab.text = _subjectName;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0;
}

- (void)selectDate
{
    UIView* sourceView = [self topView];
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                               0,
                                                               sourceView.frame.size.width,
                                                               sourceView.frame.size.height - 216 - 46 - 45)];
    [_backgroundView setBackgroundColor:[UIColor blackColor]];
    _backgroundView.alpha = 0.5;
    [sourceView addSubview:_backgroundView];
    [self initDatePickerView];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    BOOL bseleCla = NO;
    if (0 == indexPath.row) {
        bseleCla = NO;
        ApplyForClaLeaTeaListViewController* appForClassViewCtrl = [[ApplyForClaLeaTeaListViewController alloc] init];
        appForClassViewCtrl.strNavTitle = @"点名老师";
        appForClassViewCtrl.bCallName = YES;
        appForClassViewCtrl.delegate = self;
        appForClassViewCtrl.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:appForClassViewCtrl animated:YES];
    }
    else if (1 == indexPath.row)
    {
        bseleCla = YES;
        [self selectDate];
    }
    else if (2 == indexPath.row)
    {
        bseleCla = NO;
        
        if (0 == _subjectTeaLab.text.length) {
            [SVProgressHUD showErrorWithStatus:@"请选择要点名的老师"];
            return;
        }
        ApplyForClassLeaveViewController* appForClassViewCtrl = [[ApplyForClassLeaveViewController alloc] init];
        appForClassViewCtrl.appForTeaInfo = _appClaTeaInfo;
        appForClassViewCtrl.delegate = self;
        appForClassViewCtrl.hidesBottomBarWhenPushed = YES;
        appForClassViewCtrl.bCallName = YES;
        appForClassViewCtrl.sTeaName = _subjectTeaLab.text;
        [self.navigationController pushViewController:appForClassViewCtrl animated:YES];
//        ASTeaSelectClassOrSubjectViewController* appForClassViewCtrl = [[ASTeaSelectClassOrSubjectViewController alloc] initWithSubOrClass:bseleCla];
//        appForClassViewCtrl.strDate = _subjectTimeLab.text;
//        appForClassViewCtrl.delegate = self;
//        appForClassViewCtrl.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:appForClassViewCtrl animated:YES];
    }
    
//    ASTeaSelectClassOrSubjectViewController* appForClassViewCtrl = [[ASTeaSelectClassOrSubjectViewController alloc] initWithSubOrClass:bseleCla];
//    appForClassViewCtrl.strDate = _sDate;
//    appForClassViewCtrl.delegate = self;
//    appForClassViewCtrl.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:appForClassViewCtrl animated:YES];
    
}

-(void)getCourseInfo:(SubClassInfo*)classInfo;
{
    self.subClaInfo = classInfo;
    [self.tableView reloadData];
}

//ApplyForClaLeaTeaListViewControllerDelegate
-(void)getCallNameTeaInfo:(AppForClaTeaInfo*)appClaTeaCherInfo
{
    _appClaTeaInfo = appClaTeaCherInfo;
    _subjecteaName = appClaTeaCherInfo.userName;
    _subjectName = @"";
    [self.tableView reloadData];
}

//
-(void)getAppForClaCourseInfo:(ApplyForClassInfo*)appClassInfo
{
    _appClaClaInfo = appClassInfo;
    NSString *sWeek = [_weekDic objectForKey:_appClaClaInfo.weekDay];
    NSString *day = [self getday:_appClaClaInfo.courseNumber];
    //_subjectName = [NSString stringWithFormat:@"(%@.%@)%@",sWeek,_appClaClaInfo.courseNumber,_appClaClaInfo.courseName];
    _subjectName = [NSString stringWithFormat:@"(%@%@)%@",sWeek,day,_appClaClaInfo.courseName];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSString*)getday:(NSString*)sCourseNumber
{
    if (!sCourseNumber ||(0 == sCourseNumber.length))
    {
        return @"";
    }
    
    long courseNumber = [sCourseNumber integerValue];
    
    if ((0 < courseNumber) && (5 > courseNumber))
    {
        return @"上午";
    }
    
    if ((4 < courseNumber) && (9 > courseNumber))
    {
        return @"下午";
    }
    
    if ((8 < courseNumber) && (13 > courseNumber))
    {
        return @"晚上";
    }
    
    return @"";
}

@end
